﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Activities.Presentation.Model;
using System.Dynamic;

namespace Vision.Core.ActivityComponents.Collections
{
    // Interaction logic for DictionaryBuilderDesigner.xaml
    public partial class DictionaryBuilderDesigner
    {
        ICommand _addCommand = null;
        ICommand _removeCommand = null;

        public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                    _addCommand = new DelegateCommand(AddDictionaryItem);

                return _addCommand;
            }
        }

        public ICommand RemoveCommand
        {
            get
            {
                if (_removeCommand == null)
                    _removeCommand = new DelegateCommand(RemoveDictionaryItem);

                return _removeCommand;
            }
        }

        public DictionaryBuilderDesigner()
        {
            InitializeComponent();
        }

        private void AddDictionaryItem(object parameter)
        {
            try
            {
                if (this.ModelItem.ItemType.FullName.Contains("DictionaryBuilder"))
                {
                    this.ModelItem.ItemType.InvokeMember("AddToDictionary", System.Reflection.BindingFlags.InvokeMethod,
                        null, this.ModelItem.GetCurrentValue(), null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void RemoveDictionaryItem(object parameter)
        {
            try
            {
                ModelProperty dictionaryProperty = this.ModelItem.Properties["DictionaryList"];

                dictionaryProperty.Collection.Remove(parameter);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /*
        private object GetDefault(Type type)
        {
            return (type.IsValueType ? Activator.CreateInstance(type) : null);
        }
        */
    }

    /*
    public class AddDictionaryCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return (parameter is ModelItem);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            ModelProperty dictionaryProperty = (parameter as ModelItem).Properties["Dictionary"];

        }
    }
    */

    public class DelegateCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public event EventHandler CanExecuteChanged;

        public DelegateCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public DelegateCommand(Action<object> execute,
                       Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }

            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
}
